package indiwood.controller;

import indiwood.model.NoticeListVO;
import indiwood.model.NoticeVO;
import indiwood.model.service.NoticeService;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
@RequestMapping("/notice")
public class NoticeController extends MultiActionController {
	@Resource(name = "noticeServiceImpl")
	private NoticeService noticeService;
	@RequestMapping("/list.do")
	public ModelAndView noticeList(String pageNo) {
		NoticeListVO listvo = noticeService.noticeList(pageNo);
		return new ModelAndView("list.notice", "lvo", listvo);
	}
	@RequestMapping("/write_view.do")
	public ModelAndView writeView(NoticeVO mvo) {
		return new ModelAndView("write_view.notice");
	}
	@Resource(name = "uploadPath")
	private String path;
	@RequestMapping(value = "/write.do", method = RequestMethod.POST)
	public ModelAndView write(NoticeVO nvo, MultipartFile uploadFile) {
		if (uploadFile != null && !uploadFile.isEmpty()) { // 파일이 존재
			File copyFile = new File(path + "notice\\"
					+ uploadFile.getOriginalFilename()); // 업로드한 파일을 복사해서 경로로 이동
			nvo.setImagePath(uploadFile.getOriginalFilename());
			try {
				uploadFile.transferTo(copyFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		noticeService.write(nvo);
		return new ModelAndView("redirect:/notice/showContentNoHit.do?no=" + nvo.getNo());
	}
	@RequestMapping("/content.do")
	public ModelAndView showContent(
			int no,
			@CookieValue(value = "springboard2", required = false) String cookieValue,
			HttpServletResponse response) {
		NoticeVO nvo = null;
		if (cookieValue == null) {
			// log.info("springboard2 cookie 존재하지 않으므로 cookie 생성하고 count 증가");
			response.addCookie(new Cookie("springboard2", "|" + no + "|"));
			nvo = noticeService.showContent(no);
		} else if (cookieValue.indexOf("|" + no + "|") == -1) {
			// log.info("springboard2 cookie 존재하지만 "+no+"번글은 처음 조회하므로 count증가");
			cookieValue += "|" + no + "|";
			// 글번호를 쿠키에 추가
			response.addCookie(new Cookie("springboard2", cookieValue));
			nvo = noticeService.showContent(no);
		} else {
			// log.info("springboard2 cookie 존재하고 이전에 해당 게시물 읽었으므로 count 증가x");
			nvo = noticeService.showContentNoHit(no);
		}
		return new ModelAndView("content.notice", "nvo", nvo);
	}
	@RequestMapping("/showContentNoHit.do")
	public ModelAndView showContentNoHit(int no) {
		return new ModelAndView("content.notice", "nvo", noticeService.showContentNoHit(no));
	}
	@RequestMapping(value = "/update.do", method = RequestMethod.POST)
	public ModelAndView updateBoard(NoticeVO nvo, MultipartFile uploadFile) {
		System.out.println(nvo.getImagePath());
		if (uploadFile != null && !uploadFile.isEmpty()) {
			File filePath = new File(path + "notice\\" + nvo.getImagePath());
			filePath.delete();
			File copyFile = new File(path + "notice\\"
					+ uploadFile.getOriginalFilename()); // 업로드한 파일을 복사해서 경로로 이동
			nvo.setImagePath(uploadFile.getOriginalFilename());
			try {
				uploadFile.transferTo(copyFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		noticeService.updateBoard(nvo);
		return new ModelAndView("content.notice", "nvo", noticeService.showContentNoHit(nvo.getNo()));
	}
	@RequestMapping("/updateView.do")
	public ModelAndView updateView(int no, String imagePath) {
		// System.out.println(imagePath);
		return new ModelAndView("update.notice", "nvo", noticeService.showContentNoHit(no));
	}
	@RequestMapping("/deleteBoard.do")
	public ModelAndView deleteBoard(String no, String imagePath) {
		/*
		 * System.out.println("test1"); imagePath=
		 * "C:\\java-kosta\\indiwood-workspace\\indiwood\\WebContent\\upload\\magazine\\c.jpg"
		 * ; System.out.println("test2"); String fp[] = imagePath.split("/");
		 * String fpath =path+"magazine\\.jpg"; for(int i=1;i<fp.length;i++){
		 * fpath+=fp[i]; if(i<fp.length-1) fpath+="magazine\\.jpg"; }
		 * System.out.println("test3"); File file = new File(fpath);
		 * if(file.exists()) file.delete(); System.out.println(imagePath);
		 * System.out.println(fpath);
		 */
		//System.out.println(imagePath);
		//System.out.println(no);
		File file = new File(path + "notice\\" + imagePath);
		if (file.exists()) {
			file.delete();
		}
		noticeService.deleteBoard(no);
		return new ModelAndView("list.magazine", "lvo", noticeService.noticeList("1"));
	}
}